(function(){
    angular.module("productCtrlModule",[])
        .controller("productController",["$scope",
            "getDataService","IMAGEURL","contactService",
            "$stateParams","$ionicSlideBoxDelegate",
            "$timeout","bookCartDataService",
            "$ionicModal","modalDataService","USERDATA",
            function($scope,getDataService,IMAGEURL,contactService,
                $stateParams,$ionicSlideBoxDelegate,
                $timeout,bookCartDataService,$ionicModal,
                modalDataService,USERDATA
                ){
                $scope.IMAGEURL = IMAGEURL;
                getDataService.getRequest(
                    "book.php",
                    null,
                    function(data){
                        console.log(data);
                        if(data.code==0){
                            $scope.books = data.data;
                            // console.log($scope.books[]);
                        }else {
                            console.log(data.data);
                        }
                    },
                    function(error){
                        console.log(error);
                    }
                )
                $timeout(function(){
                        $ionicSlideBoxDelegate.enableSlide(true);
                    },500);

                    $scope.pagerClick = function(index){
                        console.log(index);
                        $ionicSlideBoxDelegate.slide(index,2000);
                    };

                    $scope.nextPage = function(){
                        $ionicSlideBoxDelegate.next();
                    };

                    $scope.previousPage = function(){
                        $ionicSlideBoxDelegate.previous();
                    };
                    $scope.onSlideChanger = function(index){

                    };
                    // setInterval($scope.nextPage,5000);


                    $scope.addToCart = function(book) {
                            console.log(contactService.cartList[book.id]);
                        if (contactService.cartList[book.id]) {
                            contactService.cartList[book.id].num++;
                            console.log(contactService.cartList[book.id]);
                        } else {
                            contactService.cartList[book.id] = {
                                id:book.id,
                                price:book.price,
                                title:book.title,
                                num:1
                        }
                    }
                }
                $scope.addToCart = function(book) {
                    if (bookCartDataService.cartList[book.id]) {
                          bookCartDataService.cartList[book.id].num++;
                          console.log(bookCartDataService.cartList[book.id].num);
                    } else {
                        bookCartDataService.cartList[book.id] = {
                            id:book.id,
                            price:book.price,
                            title:book.title,
                            num:1
                        }
                        console.log(bookCartDataService.cartList[book.id].num);
                    }
                }
                $scope.loginPageShow = function(){
                    $ionicModal.fromTemplateUrl('tpl/login.html', {
                        animation: 'slide-in-up'
                    }).then(function(modal) {
                        $scope.modal = modal;
                        modalDataService.myModal = modal;
                        $scope.modal.show();
                    });
                };
                 $scope.loginOut = function(){
                    $scope.isLogin = false;
                    USERDATA.isLogin = false;
                    USERDATA.user = null;
                    $scope.$broadcast("isLogin");
                    
                };
                $scope.$on("isLogin",function(){
                    console.log("hehe");
                        USERDATA.isLogin = false;
                        $scope.isLogin = false;
                        USERDATA.user = null;
                        $scope.username = null;
                    })

                // 监听 登录成功的通知
                $scope.$on("loginSuccess",function(){
                    console.log("loginSuccess");
                    USERDATA.isLogin = true;
                    $scope.isLogin = true;
                    // $scope.$broadcast("isLogin");
                    $scope.username = USERDATA.user.user_name;
                });
                    
            }
        ])
})();